﻿using System.Windows;
using ComicQ.Controller;
using ComicQ.Domain.Table;

namespace ComicQ.Dektop.View
{
    /// <summary>
    /// Interaction logic for PublisherEditorView.xaml
    /// </summary>
    public partial class PublisherEditorView
    {
        private readonly PublisherController _publisherControl;
        private readonly Publisher _publisher;

        public PublisherEditorView()
        {
            InitializeComponent();

            _publisherControl = new PublisherController();
            Title = "Add Publisher";
        }

        public PublisherEditorView(Publisher publisher)
        {
            InitializeComponent();
            _publisherControl = new PublisherController();
            Title = "Edit Publisher";

            _publisher = publisher;

            textBoxId.Text = publisher.Id.ToString();
            textBoxName.Text = publisher.Name;
            textBoxAdress.Text = publisher.Address;
        }

        private void ClearTextBox()
        {
            textBoxId.Text = "";
            textBoxName.Text = "";
            textBoxAdress.Text = "";
        }

        private void ButtonSaveClick(object sender, RoutedEventArgs e)
        {
            if (textBoxName.Text.Equals("") || textBoxAdress.Text.Equals(""))
            {
                MessageBox.Show("field tidak boleh kosong");
            }
            else
            {
                if (Title.Equals("Add Publisher"))
                {
                    var publisher = new Publisher
                                     {
                                         Name = textBoxName.Text,
                                         Address = textBoxAdress.Text
                                     };

                    var message = _publisherControl.AddPublisher(publisher);

                    switch (message)
                    {
                        case 1:
                            MessageBox.Show("sukses menambah publisher");
                            ClearTextBox();
                            break;
                        default:
                            MessageBox.Show("nama sudah ada");
                            break;
                    }
                }
                else
                {
                    var newPublisher = new Publisher
                                        {
                                            Id = _publisher.Id,
                                            Name = textBoxName.Text,
                                            Address = textBoxAdress.Text
                                        };

                    var message = _publisherControl.EditPublisher(newPublisher, _publisher.Name);

                    switch (message)
                    {
                        case 1:
                            MessageBox.Show("sukses mengedit publisher");
                            break;
                        default:
                            MessageBox.Show("nama sudah ada");
                            break;
                    }
                }
            }
        }

        private void ButtonCancelClick(object sender, RoutedEventArgs e)
        {
            Close();
        }
    }
}
